/**
 * 本地记录类
 */
export class Recorder {
    static _instance: Recorder = null;
    static getInstance(): Recorder {
        if (!Recorder._instance) {
            Recorder._instance = new Recorder();
        }
        return Recorder._instance;
    }
    getData(key) {
        let val = recorder.getItem(key)
        if (!recorder.isNormalVal(key)) {
            console.error('getData 非正常key', key)
            return null;
        }
        let obj;
        try {
            obj = JSON.parse(val);
        } catch (e) {
            obj = val;
        }
        return obj;
    }

    setData(key, value) {
        if (recorder.isNormalVal(key)) {
            if (typeof value == 'object') {
                recorder.setItem(key, JSON.stringify(value))
            } else if (typeof value == 'number') {
                recorder.setItem(key, value)
            } else if (typeof value == 'boolean') {
                recorder.setItem(key, value)
            } else if (typeof value == 'string') {
                recorder.setItem(key, value)
            }
        } else {
            console.error('setData 非正常key', key)
        }
    }

    removeAll() {
        // if (_PF.is_wx()) {
        //     wx.clearStorageSync();
        // } else if (_PF.is_vivo()) {
        //     // 同步删除所有存储内容
        //     qg.clearStorageSync();
        // } else if (_PF.is_oppo() || _PF.is_huawei()) {
        //     // 调用该方法会清空存储中的所有键名。
        //     localStorage.clear();
        // } else if (_PF.is_bd()) {
        //     // 同步清理本地数据缓存
        //     swan.clearStorageSync();
        // } else if (_PF.is_qq_new()) {
        //     qq.clearStorageSync();
        // } else if (_PF.is_qq()) {
        //     BK.localStorage.clear();
        // } else {
        //     cc.sys.localStorage.clear();
        // }
        cc.sys.localStorage.clear();
    }

    private isNormalVal(key) {
        if (key == undefined || key == null || key == '') {
            return false;
        }
        return true;
    }

    private setItem(key, value) {
        cc.sys.localStorage.setItem(key, value)
    }
    private getItem(key): any {
        return cc.sys.localStorage.getItem(key);
    }


}
let recorder = Recorder.getInstance();
window['recorder'] = recorder;
export { recorder };
